package com.driver.core;

import java.util.List;

public class StringUtil {


    public static String columnNametoPropertyName(String column) {
        String newColumn;
        if (column.contains("_")) {
            StringBuilder sb = getSplit(column);
            newColumn = lowerFirstName(sb.toString());
        } else {
            newColumn = lowerFirstName(column);
        }
        if(newColumn.length()>=2){
            newColumn= newColumn.substring(0, 2).toLowerCase() + newColumn.substring(2);
        }

        return newColumn;

    }


    private static StringBuilder getSplit(String value) {
        String[] split = value.split("_");
        StringBuilder sb = new StringBuilder();
        for (String value0 : split) {
            sb.append(upperFirstName(value0));
        }
        return sb;
    }


    public static String getDeleteTableNamePreffix(String tableName, String preffix) {
        if (preffix.length() >= tableName.length()) throw new RuntimeException("前缀长度大于表名的长度");
        return tableName.substring(preffix.length());
    }

    public static String upperFirstName(String name) {
        return name.substring(0, 1).toUpperCase() + name.substring(1);
    }

    public static String lowerFirstName(String name) {
        return name.substring(0, 1).toLowerCase() + name.substring(1);
    }


    public static String getImportString(String propertyType) {
        String value = "import ";
        if ("String".equals(propertyType)) {
            value = value + "java.lang.String;";
        } else if ("Integer".equals(propertyType)) {
            value = value + "java.lang.Integer;";
        } else if ("Date".equals(propertyType)) {
            value = value + "java.util.Date;";
        } else if ("BigDecimal".equals(propertyType)) {
            value = value + "java.math.BigDecimal;";
        } else if ("Long".equals(propertyType)) {
            value = value + "java.lang.Long;";
        } else if ("Short".equals(propertyType)) {
            value = value + "java.lang.Short;";
        } else if ("Boolean".equals(propertyType)) {
            value = value + "java.lang.Boolean;";
        } else if ("Byte".equals(propertyType)) {
            value = value + "java.lang.Byte;";
        } else if ("Float".equals(propertyType)) {
            value = value + "java.lang.Float;";
        } else if ("Double".equals(propertyType)) {
            value = value + "java.lang.Double;";
        }
        else if ("Timestamp".equals(propertyType)) {
            value = value + "java.sql.Timestamp;";
        }
        return value;
    }


    /**
     * 可能有问题
     *
     * @param table
     * @return
     */
    public static String getToString(Table table) {
        StringBuilder sb = new StringBuilder();
        sb.append(" @Override\n" +
                "    public String toString() {\n");
        sb.append("\treturn ");
        sb.append("\"").append(table.getClassName()).append("{");
        List<Column> columns = table.getColumns();
        for (Column column : columns) {
            sb.append(column.getPropertyName()).append("=\"+").append(column.getPropertyName()).append("+\",");
        }
        return sb.substring(0, sb.length() - 2) + "'}';" + "\n\t}";
    }

    public static String tableNameToClassName(String tableName) {
        if (tableName.contains("_")) {
            StringBuilder sb = getSplit(tableName);
            return upperFirstName(sb.toString());
        } else {
            return upperFirstName(tableName);
        }
    }
}
